package com.yyds.blackmagic.controller;

import com.yyds.blackmagic.entity.Article;
import com.yyds.blackmagic.service.ArticleService;
import com.yyds.blackmagic.vo.ActionResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("/articles")
    public ActionResult findAll(){
        List<Article> all = articleService.findAll();
        return new ActionResult(200,"查询所有成功",all);
    }
    @GetMapping("/articles2")
    public ActionResult findAllByTime(){
        List<Article> all1 = articleService.findAllByTime();
        return new ActionResult(200,"按时间排序查询成功",all1);
    }
    @GetMapping("/article/{article_title}")
    public ActionResult findOne(@PathVariable String article_title){
        Article one = articleService.findOneByTitle(article_title);
        return new ActionResult(200,"按标题查询成功",one);
    }
    @GetMapping("/article2/{article_content_md}")
    public ActionResult findOne2(@PathVariable String article_content_md){
        Article one1 = articleService.findOneByContent(article_content_md);
        return new ActionResult(200,"按内容查询成功",one1);
    }
    @GetMapping("/article3/{id}")
    public ActionResult findOne3(@PathVariable int id){
        Article one2 = articleService.findOneById(id);
        return new ActionResult(200,"按Id查询成功",one2);
    }
    @PostMapping("/admin/content/article")
    public ActionResult addArticle(@RequestBody Article article){
        log.info("article:"+article);
        articleService.addArticle(article);
        return new ActionResult(200,"添加成功",null);
    }
    @PutMapping("/uparticle")
    public ActionResult updateArticle( Article article){
        articleService.updateArticle(article);
        return new ActionResult(200,"修改成功",null);
    }
    @DeleteMapping("/delarticle/{id}")
    public ActionResult delArticle(@PathVariable("id") int id){
        articleService.delArticle(id);
        return new ActionResult(200,"删除成功",null);
    }
}
